﻿using AutoMapper;
using ManagementApi.Models.System.Role;
using ManagementApi.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Cms;
using Modules.System.Role;
using ManagementApi.Models.Cms;
using ManagementApi.Mvc.Authorization;
using Modules.Cms.Entitys;
using StackExchange.Redis;
using PmSoft.Utilities;
using PmSoft.ApplicationContext;
using Modules.System.User;
using PmSoft;

namespace ManagementApi.Controllers.Cms;

/// <summary>
/// 经销商相关业务接口
/// </summary>
[Route("/api/v1/cmsDealer")]
[ApiController]
[Authorize()]
public class CmsDealerController : ControllerBase
{
    private readonly CmsDealerService cmsDealerService;
    private readonly CmsOrderService cmsOrderService;
    private readonly CmsCustomLeaseService cmsCustomLeaseService;
    private readonly IApplicationContext applicationContext;
    private readonly IMapper mapper;

    public CmsDealerController(CmsDealerService cmsDealerService, CmsOrderService cmsOrderService, CmsCustomLeaseService cmsCustomLeaseService, IApplicationContext applicationContext, IMapper mapper)
    {
        this.cmsDealerService = cmsDealerService;
        this.mapper = mapper;
        this.applicationContext=applicationContext;
        this.cmsOrderService = cmsOrderService;
        this.cmsCustomLeaseService=cmsCustomLeaseService;
    }


    /// <summary>
    /// 获取经销商列表
    /// </summary>
    /// <param name="query">查询条件</param>
    /// <param name="pageNum">页码</param>
    /// <param name="pageSize">每页数量</param>
    /// <returns></returns>
    [HttpGet("page")]
    public ApiResult<PageDto<CmsDealerDto>> GetRoles([FromQuery] CmsDealerQuery query, int pageNum = 1, int pageSize = 20)
    {
        var data = cmsDealerService.GetPages(query, pageNum, pageSize);
        var cmsOrders = cmsOrderService.GetOrdersByDealerIds(data.Select(t => t.Id));
        var  customLeases = cmsCustomLeaseService.GetCmsCustomLeasesByDealerIds(data.Select(t => t.Id));
        var dtos = data.AsPageDto(source => { return source.AsDto(cmsOrders, customLeases); });
        return ApiResult<PageDto<CmsDealerDto>>.Success(dtos);
    }

    /// <summary>
    /// 创建经销商
    /// </summary>
    /// <param name="form"></param>
    /// <returns></returns>
    [HttpPost("create")]
    public ApiResult Add(CmsDealerEditForm form)
    {
        CmsDealer entity = new CmsDealer();
        mapper.Map(form, entity);
        entity.CreateDt = DateTime.Now;
        entity.CreateUserId = applicationContext.CurrentUser?.Id??0;
        entity.CreateUserName = applicationContext.CurrentUser?.Name ?? "";
        cmsDealerService.Create(entity);
        return ApiResult.Success("创建成功");
    }

    /// <summary>
    /// 修改经销商
    /// </summary>
    /// <param name="id"></param>
    /// <param name="form"></param>
    /// <returns></returns>
    [HttpPut("{id}")]
    public ApiResult Edit(int id, CmsDealerEditForm form)
    {
        CmsDealer? entity = cmsDealerService.Get(id);
        if (entity == null) return ApiResult.Failure("数据不存在");
        mapper.Map(form, entity);
        entity.UpdateDt = DateTime.Now;
        entity.UpdateUserId = applicationContext.CurrentUser?.Id ?? 0;
        entity.UpdateUserName = applicationContext.CurrentUser?.Name ?? "";
        cmsDealerService.Update(entity);
        return ApiResult.Success("编辑成功");
    }

    /// <summary>
    /// 删除经销商
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete("{id}")]
    public ApiResult Delete(int id)
    {
        cmsDealerService.Delete(id);
        return ApiResult.Success("删除成功");
    }

    /// <summary>
    /// 经销商详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public ApiResult<CmsDealerEditForm> Get(int id)
    {
        CmsDealer? entity = cmsDealerService.Get(id);
        if (entity == null) return ApiResult<CmsDealerEditForm>.Failure("数据不存在");
        CmsDealerEditForm data = mapper.Map<CmsDealerEditForm>(entity);
        data.UserName = ServiceAccessor.GetService<UserService>().Get(data.UserId)?.Name??"";
        return ApiResult<CmsDealerEditForm>.Success(data);
    }
}
